package com.lianjinsu.shop.controller;

import com.lianjinsu.shop.controller.dto.OrdersDto;
import com.lianjinsu.shop.controller.vo.Response;
import com.lianjinsu.shop.entity.Orders;
import com.lianjinsu.shop.service.OrdersService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping(value = "api")
public class OrdersController {

    private final OrdersService ordersService;

    @GetMapping("getById/{tradeSn}")
    Response<Orders> getById(@PathVariable String tradeSn) {
        return Response.ok(ordersService.getById(tradeSn));
    }

    @GetMapping("deleteById/{tradeSn}")
    Response<Boolean> deleteById(@PathVariable String tradeSn) {
        return Response.ok(ordersService.deleteById(tradeSn));
    }

    @PostMapping("update")
    Response<Boolean> update(@RequestBody Orders orders) {
        if (StringUtils.isBlank(orders.getOpenid())) {
            return Response.fail("openid is null");
        }
        return Response.ok(ordersService.update(orders));
    }

    @GetMapping("selectByUser/{openId}")
    Response<List<Orders>> selectByUser(@PathVariable String openId) {
        return Response.ok(ordersService.selectByUser(openId));
    }

    @PostMapping("generateOrder")
    Response<Map<String, Object>> generateOrder(@RequestBody OrdersDto ordersDto) {
        return Response.ok(ordersService.generateOrder(ordersDto));
    }

    @PostMapping("getOrder")
    Response<Orders> getOrder(@RequestParam String tradeSn) {
        return Response.ok(ordersService.getById(tradeSn));
    }

    @GetMapping("getOrderList")
    Response<List<Orders>> getOrderList(@RequestParam int status) {
        return Response.ok(ordersService.ordersList(status));
    }

    @GetMapping("cancelOrder")
    Response<Object> cancelOrder(@RequestParam String tradeSn) {
        return Response.ok(ordersService.cancelOrder(tradeSn));
    }

    @GetMapping("getWaitPayOrder")
    Response<List<Orders>> getWaitPayOrder() {
        return Response.ok(ordersService.waitPayOrdersList());
    }
}
